(function() {

	var appModule = angular.module('tracecashModule', [ 'ngRoute', 'ui.bootstrap', 'ui.mask', 'pascalprecht.translate', 'ngAnimate', 'tc.chartjs']);

	/**
	 * ROUTES
	 */
	appModule.config([ '$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

		$routeProvider.when('/operations/', {
			templateUrl : 'html/operation/operation-list.html',
			controller : 'OperationListCtrl'
		});
		
		
		
		
		
		
		// ---------------------------------- DEMO --------------------------------------
		
		// PREPARACIÓN DE LAS POLIZAS DESDE LOS DIFERENTES SITIOS (PASO 1 Y 2)
		$routeProvider.when('/newPolicyFront/', {
			templateUrl : 'html/sinister/sinister-new.html',
			controller : 'NewSinisterFrontCtrl'
		});
		$routeProvider.when('/newPolicyHost/:agreement/:network/:number', {
			templateUrl : 'html/sinister/sinister-new.html',
			controller : 'NewSinisterHostCtrl'
		});
		$routeProvider.when('/newPolicyPole/:poleId', {
			templateUrl : 'html/sinister/sinister-new.html',
			controller : 'NewSinisterPoleCtrl'
		});

		// ALTA Y EDICION DE SINIESTRO
		$routeProvider.when('/newSinister', {
			templateUrl : 'html/sinister/sinister.html',
			controller : 'SinisterCtrl',
			resolve: {
				sinister: ['sinisterService', function(sinisterService) {
	            	return sinisterService.prepare(sinisterService.getSinister());
	            }]
			}
		});
		$routeProvider.when('/sinister/:sinisterId', {
			templateUrl : 'html/sinister/sinister.html',
			controller : 'SinisterCtrl',
			resolve: {
	            sinister: ['$route', 'sinisterService', function($route, sinisterService) {
	            	return sinisterService.find($route.current.params.sinisterId);
	            }]
			}
		});

		// HISTORICO Y DOCUMENTACIÓN ADJUNTA
		$routeProvider.when('/historyList/:sinisterId', {
			templateUrl : 'html/sinister/sinister-history-list.html',
			controller : 'SinisterHistoryListCtrl'
		}).when('/historyDetail/:historyEntityId', {
			templateUrl : 'html/sinister/sinister.html',
			controller : 'SinisterHistoryDetailCtrl'
		});
		$routeProvider.when('/letterList/:sinisterId', {
			templateUrl : 'html/letter/letter-list.html',
			controller : 'SinisterLetterListCtrl'
		});

		// ADMINISTRACIÓN
		$routeProvider.when('/administration/agreement', {
			templateUrl : 'html/administration/agreement-list.html',
			controller : 'AgreementListCtrl'
		});
		$routeProvider.when('/administration/warrantyType', {
			templateUrl : 'html/administration/warranty-type-list.html',
			controller : 'WarrantyTypeListCtrl'
		});
		$routeProvider.when('/administration/agreementWarrantyType', {
			templateUrl : 'html/administration/agreement-warranty-type-list.html',
			controller : 'AgreementWarrantyTypeListCtrl'
		});
		$routeProvider.when('/administration/company', {
			templateUrl : 'html/administration/company-list.html',
			controller : 'CompanyListCtrl'
		});
		$routeProvider.when('/administration/subcompany', {
			templateUrl : 'html/administration/subcompany-list.html',
			controller : 'SubcompanyListCtrl'
		});
		$routeProvider.when('/administration/mailRecipient', {
			templateUrl : 'html/administration/email-list.html',
			controller : 'MailRecipientListCtrl'
		});
		$routeProvider.when('/administration/network', {
			templateUrl : 'html/administration/network-list.html',
			controller : 'NetworkListCtrl'
		});

		// REPORTS
		$routeProvider.when('/report', {
			templateUrl : 'html/report/report-detail.html',
			controller : 'ReportCtrl'
		});

		// DASHBOARD
		$routeProvider.when('/dashboard', {
			templateUrl : 'html/dashboard/dashboard-panel.html',
			controller : 'DashboardCtrl'
		});
		
		// BÚSQUEDAS
		$routeProvider.when('/search-policy', {
			templateUrl : 'html/search-policy/search-policy.html',
			controller : 'SearchPolicyCtrl'
		});
		$routeProvider.when('/search-sinister', {
			templateUrl : 'html/search-sinister/search-sinister.html',
			controller : 'SinisterSearchCtrl'
		});

		// PAGINA POR DEFECTO
		$routeProvider.when('/', {
			templateUrl : 'html/search-sinister/search-sinister.html',
			controller : 'SinisterSearchCtrl'
		});

	} ]);

	/**
	 * GENERAL CONTROLLES AND GLOBAL VARIABLES
	 */
	appModule.controller('MainCtrl', [ '$scope', '$rootScope', '$location', function($scope, $rootScope, $location) {
		$rootScope.go = function(hash) {
			$location.path(hash);
		};
	} ]);

	appModule.controller('MenuCtrl', [ '$scope', '$rootScope', '$location', function($scope, $rootScope, $location) {
		$scope.menu = '';
		$scope.setMenu = function(menu) {
			$scope.menu = menu;
		};
		$scope.getMenu = function() {
			return $scope.menu;
		};
	} ]);

	appModule.run(['$rootScope', function($rootScope) {
		$rootScope.REST_PATH = REST_PATH;
		$rootScope.CONTEXT_PATH = CONTEXT_PATH;
		$rootScope.CODE_SUCCESS = 200;
		$rootScope.PAGE_SIZE = 10;
		$rootScope.SEARCH_PAGE_SIZE = 20;
	}]);

	/**
	 * LANGUAGE CONFIGURATION
	 */
	appModule.config(['$provide', function($provide) {
		$provide.decorator('tabDirective', ['$delegate', function($delegate) {
			var directive = $delegate[0];
			angular.extend(directive.scope, {
				ngModel : '=ngModel'
			});
			return $delegate;
		}]);
	}]);

	appModule.config(['$translateProvider', function($translateProvider) {
		$translateProvider.preferredLanguage('es');
		$translateProvider.useUrlLoader(REST_PATH + '/i18n/json');
	}]);

	appModule.controller('LanguageCtrl', ['$scope', '$translate', function($scope, $translate) {
		$scope.changeLanguage = function(key) {
			$translate.use(key);
		};
		$scope.getLanguage = function() {
			return $translate.use();
		};
	}]);

	/**
	 * ACCOUNT INFO
	 */
	appModule.controller('AccountCtrl', ['$scope', '$rootScope', '$http', '$translate', '$window', function($scope, $rootScope, $http, $translate, $window) {
		$http.get($rootScope.REST_PATH + '/security/loggedUser').success(function(data) {
			$scope.userName = data;
		});
		$scope.logout = function() {
			$http.get($rootScope.REST_PATH + '/security/logout').success(function(url) {
				$window.location = url;
			});
		};
	}]);

})();